package org.example;

/**
 * 给定一个整数n，返回 n! 结果尾数中零的数量
 *
 * 解题思路(2 * 5)产生0
 */
public class TrailingZeroesSolution {

    public int trailingZeroes(int n) {
        if (n < 1) {
            return 0;
        }

        int num2Count = 0;
        int num5Count = 0;

        int tempN = 1;
        while (tempN <= n) {
            num2Count += getNumCount(tempN, 2);
            num5Count += getNumCount(tempN, 5);
            tempN ++;
        }

        int zeroCount = 0;
        if (num2Count > num5Count) {
            zeroCount = num5Count;
        } else {
            zeroCount = num2Count;
        }
        return zeroCount;
    }

    private int getNumCount(int num, int baseNum) {
        int count = 0;
        int tempNum = num;
        while (tempNum % baseNum == 0) {
            tempNum = tempNum / baseNum;
            count ++;
        }
        return count;
    }

    public static void main(String[] args) {
        int num = 10;
        TrailingZeroesSolution solution = new TrailingZeroesSolution();
        System.out.println(solution.trailingZeroes(num));
    }
}
